package com.grendel.tubeyourmind.youtube
{
	import com.grendel.tubeyourmind.core.SimpleRequest;
	import com.grendel.tubeyourmind.youtube.events.YoutubeEvent;
	import com.grendel.tubeyourmind.youtube.vo.Playlist;
	import com.grendel.tubeyourmind.youtube.vo.Video;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class Youtube implements IEventDispatcher
	{
		private var _ed:EventDispatcher;

		private var _playlist:Playlist;
		
		private var _playlists:Array;
		
		private const YOUTUBE_GET_PLAYLISTS_REQUEST:String = "http://gdata.youtube.com/feeds/api/users/%USERNAME%/playlists?v=2";
		private const YOUTUBE_GET_PLAYLIST_REQUEST:String = "http://gdata.youtube.com/feeds/api/playlists/%PLAYLIST_ID%?v=2"
		
		public static const NS_DEFAULT:Namespace = new Namespace("http://www.w3.org/2005/Atom");
		public static const NS_MEDIA:Namespace = new Namespace("http://search.yahoo.com/mrss/");
		public static const NS_YT:Namespace = new Namespace("http://gdata.youtube.com/schemas/2007");

		public function get playlist():Playlist
		{
			return _playlist;
		}

		public function set playlist(value:Playlist):void
		{
			_playlist = value;
		}
		
		private var _videos:Array;
		
		public function get videos():Array
		{
			return _videos;
		}

		public function Youtube()
		{
			_ed = new EventDispatcher(this);
		}
		
		public function loadPlaylists(userName:String):void
		{
			SimpleRequest.doRequest
			(
				YOUTUBE_GET_PLAYLISTS_REQUEST.replace("%USERNAME%", userName),
				function (event:Event):void
				{
					_playlists = new Array();
					var entries:XMLList = XML(event.target.data)..NS_DEFAULT::entry;
					
					var entry:XML = new XML();
					for each (var c:XML in entries)
					{
						_playlists.push(
							new Playlist
							(
								c.NS_YT::playlistId,
								c.NS_DEFAULT::title,
								c.NS_DEFAULT::summary,
								null,
								null
							)
						);
					}
					
					dispatchEvent(new YoutubeEvent(YoutubeEvent.PLAYLISTS_LOADED));
				}
			);
		}
		
		public function loadVideosFromPlaylist(userName:String, playlist:String):void
		{
			SimpleRequest.doRequest
			(
				YOUTUBE_GET_PLAYLISTS_REQUEST.replace("%USERNAME%", userName),
				function (event:Event):void
				{
					_playlists = new Array();
					var entries:XMLList = XML(event.target.data)..NS_DEFAULT::entry;
					
					var entry:XML = new XML();
					for each (var c:XML in entries)
					{
						if (c.NS_DEFAULT::title == playlist)
						{
							entry = c;
							break;
						}
						
					}
					
					loadVideos(entry.NS_YT::playlistId);
				}
			);
		}
		
		private function loadVideos(playlistId:String):void
		{
			SimpleRequest.doRequest
			(
				YOUTUBE_GET_PLAYLIST_REQUEST.replace("%PLAYLIST_ID%", playlistId),
				function (event:Event):void
				{
					_videos = new Array();
					var entries:XMLList = XML(event.target.data)..NS_DEFAULT::entry;
					
					var entry:XML = new XML();
					for each (var c:XML in entries)
					{
						_videos.push( new Video(
							c.NS_MEDIA::group.NS_YT::videoid,
							c.NS_DEFAULT::title
						));
					}
					
					dispatchEvent(new YoutubeEvent(YoutubeEvent.VIDEOS_FROM_PLAYLIST_LOADED));
				}
			);
		}

		//IMPLEMENT IEventDispatcher functions
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			_ed.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			_ed.removeEventListener(type, listener, useCapture);
		}
		public function dispatchEvent(event:Event):Boolean
		{
			return _ed.dispatchEvent(event);
		}
		public function hasEventListener(type:String):Boolean
		{
			return _ed.hasEventListener(type);
		}
		public function	willTrigger(type:String):Boolean
		{
			return _ed.willTrigger(type);
		}
	}
}